

#import "PosterView.h"
#import "MovieModel.h"
#import "RatingView.h"
@implementation PosterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initHeaderView];
        [self initContentView];
        [self initFooterView];
        [self bringSubviewToFront:_headerView];
    }
    return self;
}

- (void)initHeaderView
{
    _headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -100, kDeviceWidth, 126)];
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];
    _headerView.image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:1];
    [self addSubview:_headerView];
    _headerView.userInteractionEnabled = YES;
    
    _topButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _topButton.frame = CGRectMake((_headerView.width/2.0-13/2.0), 107, 13, 13);
    [_topButton setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    [_topButton addTarget:self action:@selector(upOrDown) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:_topButton];
    
    _topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 100)];
    _topScrollView.decelerationRate = 0.1;
    _topScrollView.showsHorizontalScrollIndicator = NO;
    [_headerView addSubview:_topScrollView];
    _topScrollView.delegate = self;
}

- (void)initContentView
{
    _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _headerView.bottom, kDeviceWidth, kDeviceHeight-20-44-49-26-40)];
    //_contentView.backgroundColor = [UIColor redColor];
    [self addSubview:_contentView];
    
    _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 0, kDeviceWidth-60, _contentView.height)];
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    _contentScrollView.pagingEnabled = YES;
    _contentScrollView.clipsToBounds = NO;
    [_contentView addSubview:_contentScrollView];
    _contentScrollView.delegate = self;
}

- (void)initFooterView
{
    _footerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, _contentView.bottom, kDeviceWidth, 40)];
    _footerView.image = [UIImage imageNamed:@"poster_title_home"];
    [self addSubview:_footerView];
    
    _footerLabel = [[UILabel alloc] initWithFrame:_footerView.bounds];
    _footerLabel.textAlignment = NSTextAlignmentCenter;
    _footerLabel.textColor = [UIColor whiteColor];
    _footerLabel.font = [UIFont boldSystemFontOfSize:24];
    [_footerView addSubview:_footerLabel];
    
}

- (void)reloadPosterData:(NSArray *)data
{
    _posterData = data;
    [self setContent];
}

- (void)setContent
{
    MovieModel *model = _posterData[0];
    
    _footerLabel.text = model.title;
    int x=0,pag = 10,left=6;
    for (int index=0; index<_posterData.count; index++)
    {
        MovieModel *movieModel = _posterData[index];
        UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(pag+x, 10, _contentScrollView.width-2*pag, _contentScrollView.height-20)];
        baseView.tag  = 105;
        baseView.userInteractionEnabled = YES;
        [_contentScrollView addSubview:baseView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(exchangeSubviews:)];
        [baseView addGestureRecognizer:tap];
        
        UIView *detailView = [[UIView alloc] initWithFrame:baseView.bounds];
        detailView.backgroundColor = [UIColor orangeColor];
        [baseView addSubview:detailView];
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 100, 150)];
        NSString *imageUrl = [movieModel.images objectForKey:@"medium"];
        [image setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"12"]];
        [detailView addSubview:image];
        
        UILabel *chinese = [[UILabel alloc] initWithFrame:CGRectMake(image.right+10, image.top, detailView.width-130, 45)];
        chinese.numberOfLines = 0;
        chinese.text = [NSString stringWithFormat:@"中文:%@",movieModel.title];
        chinese.font = [UIFont boldSystemFontOfSize:18];
        [detailView addSubview:chinese];
        
        UILabel *english = [[UILabel alloc] initWithFrame:CGRectMake(chinese.left, chinese.bottom+10, detailView.width-130, 45)];
        english.numberOfLines = 0;
        english.text = [NSString stringWithFormat:@"英文:%@",movieModel.english];
        english.font = [UIFont boldSystemFontOfSize:16];
        [detailView addSubview:english];
        
        UILabel *year = [[UILabel alloc] initWithFrame:CGRectMake(chinese.left, english.bottom+10, detailView.width-130, 45)];
        year.text = [NSString stringWithFormat:@"年份:%@",movieModel.year];
        year.font = [UIFont boldSystemFontOfSize:18];
        [detailView addSubview:year];
        
        RatingView *rating = [[RatingView alloc] initWithFrame:CGRectMake(left, detailView.height*2/3, 0, 0)];
        rating.ratingData = [[movieModel.rating objectForKey:@"average"] floatValue];
        rating.style = kNormlStyle;
        [detailView addSubview:rating];
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:baseView.bounds];
        NSString *imgUrl = [movieModel.images objectForKey:@"large"];
        [imgView setImageWithURL:[NSURL URLWithString:imgUrl]];
        [baseView addSubview:imgView];
        
        x+=_contentScrollView.width;
    }
    _contentScrollView.contentSize = CGSizeMake(x, _contentScrollView.height);
}

- (void)exchangeSubviews:(UITapGestureRecognizer *)tap
{
    UIView *view = [tap view];
    //block 动画
    [UIView animateWithDuration:0.33 animations:^{
        [view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        
        if (view.tag==105)
        {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];
            view.tag = 106;
        }
        else
        {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:YES];
            view.tag = 105;
        }
    }];
}

- (void)upOrDown
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    if (_headerView.top == -100) {
        _headerView.top = 0;
        [_topButton setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateNormal];
        
        [self initMaskView];
        
        if (_topScrollView.subviews.count==0 || _topScrollView.subviews.count==1) {
            int x=0;
            for (int index=0; index<_posterData.count; index++) {
                MovieModel *movieModel = _posterData[index];
                UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(130+x, 5, 60, 90)];
                NSString *imgUrl = [movieModel.images objectForKey:@"medium"];
                [img setImageWithURL:[NSURL URLWithString:imgUrl]];
                [_topScrollView addSubview:img];
                x+=70;
            }
            _topScrollView.contentSize = CGSizeMake(x+260, _topScrollView.height);
        }
        
    }else{
        _headerView.top = -100;
        [_topButton setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
        
        _maskView.backgroundColor = [UIColor clearColor];
    
        [_maskView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.4];
    }
    
    [UIView commitAnimations];
}

- (void)initMaskView
{
    _maskView = [[UIView alloc] initWithFrame:self.bounds];
    _maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self insertSubview:_maskView aboveSubview:_contentView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(upOrDown)];
    [_maskView addGestureRecognizer:tap];
}

#pragma mark 滑动视图的协议方法
- (void)_scrollViewDidScroll:(UIScrollView *)scrollView
{
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationDuration:0.5];
    //水平偏移量
    int index=0;
    if (scrollView == _topScrollView)
    {
        index = floor((_topScrollView.contentOffset.x - 35)/70) + 1;
        _contentScrollView.contentOffset = CGPointMake(index*_contentScrollView.width, 0);
        
    }
    else
    {
        index = (_contentScrollView.contentOffset.x+_contentScrollView.width*0.5) /_contentScrollView.width;
        
    }
    _topScrollView.contentOffset = CGPointMake(index*70, 0);
    _footerLabel.text = [_posterData[index] title];
    [UIView commitAnimations];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self _scrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self _scrollViewDidScroll:scrollView];
    }
}

@end
